trigger CaseInput on Case (before insert, before update) {
    List<Case> oldRecords = trigger.old;
    List<Case> triggeredRecords = trigger.new;
    
    if (trigger.isBefore && !trigger.isUpdate) {
        Integer currentMinutes = Integer.valueOf(DateTime.now().format('m')) + 1;
        if (currentMinutes >= 60) {
            currentMinutes -= 60;
        }
        List<EZAssignJobIDs__c> jobRecords = [select JobID__c, Name from EZAssignJobIDs__c where Name = 'Update Cases' limit 1];
        EZAssignJobIDs__c jobRecord;
        if (jobRecords.size() > 0) {
        	jobRecord = jobRecords[0];
        } else {
        	jobRecord = new EZAssignJobIDs__c();
            jobRecord.Name = 'Update Cases';
            jobRecord.JobID__c = '000000000000000';
        }
        List<CronTrigger> scheduledJobs = [select Id from CronTrigger where Id = :jobRecord.JobID__c];
        
        if (scheduledJobs.size() == 0) {
        	String jobId = System.schedule('Update Cases', '0 ' + currentMinutes + ' * * * ?', new SchedulerClass());
            jobRecord.JobID__c = jobId;
            upsert jobRecord;
        }
    }
    
    if (trigger.isAfter || trigger.isUpdate) {
        Map<Id,Case> oldRecordsMap = new Map<Id,Case>();
        if (oldRecords != null) {
            for (Case item: oldRecords) {
                oldRecordsMap.put(item.Id,item);
            }
        }
        
        List<QueueSetting__c> qsList = [select Id,Active__c,QueueId__c from QueueSetting__c where Active__c = true];
        Map<Id,QueueSetting__c> qsToQid = new Map<Id,QueueSetting__c>();
        for (QueueSetting__c item: qsList) {
            qsToQid.put(item.QueueId__c,item);
        }
        
        List<Case> recordsToProcess = new List<Case>();
        
        List<EZAssignSettings__c> afs = [select ObjectAPIName__c, ActiveFieldValues__c, ActiveFieldAPIName__c 
                                                from EZAssignSettings__c
                                                where ObjectAPIName__c = 'Case' limit 1];
        String field;
        String value;
        if (afs.size() > 0) {
            value = afs[0].ActiveFieldValues__c;
            field = afs[0].ActiveFieldAPIName__c;
        }
        Boolean isActiveRecord;
        
        for (Case item: triggeredRecords) {
            if (qsToQid.get(item.OwnerId) != null) {
                isActiveRecord = false;
                if (value != null) {
                    List<String> valueParts = ((String)value).split(',');
                    for (String val: valueParts) {
                        try {
                            if (item.get(field) == val) {
                                isActiveRecord = true;
                            }
                        } catch (Exception ex) {}
                    }
                    if (isActiveRecord) {
                        recordsToProcess.add(item);
                    }
                }
            }
        }
        
        //ProcessRecords batchController = new ProcessRecords(recordsToProcess,false,true);
        Integer currentMinutes = Integer.valueOf(DateTime.now().format('m')) + 1;
        if (currentMinutes >= 60) {
            currentMinutes -= 60;
        }
            
        List<EZAssignJobIDs__c> jobRecords = [select JobID__c, Name from EZAssignJobIDs__c where Name = 'Update Cases' limit 1];
        EZAssignJobIDs__c jobRecord;
        if (jobRecords.size() > 0) {
        	jobRecord = jobRecords[0];
        } else {
        	jobRecord = new EZAssignJobIDs__c();
            jobRecord.Name = 'Update Cases';
            jobRecord.JobID__c = '000000000000000';
        }
        List<CronTrigger> scheduledJobs = [select Id from CronTrigger where Id = :jobRecord.JobID__c];
        
        if (scheduledJobs.size() == 0) {
        	String jobId = System.schedule('Update Cases', '0 ' + currentMinutes + ' * * * ?', new SchedulerClass());
            jobRecord.JobID__c = jobId;
            upsert jobRecord;
        }
    }
}